/*

function xxx_findByField(m,field,data) {
    var x = xxx_findByModel(m);
    for (i = 0; i < users.length; i++) {
        var o = x[i];
    }
    var a = [];    
    if (o.get(field) === data) {
        a.push(o);
    }
    return a;
}*/
//function modelName2TableName(m) {
  //  return m.toLowerCase(m);
//}

App.RESTAdapter = Ember.Object.extend({
    find: function(mod, obj) {
        if (obj !== null) {
            // find a single object
            return $.post(App.Config.server,
                    {action: 'read', table: mod.mclass, field: 'id', data: obj.id},
            "json").then(function(data) {
                var arr = $.parseJSON(data);
                var o = mod.create(arr[0]);
                return o;
            });
        };
        // find all objects
        return $.post(App.Config.server,
                {action: 'read', table: mod.mclass},
        "json").then(function(data) {
            var s = [];
            var arr = $.parseJSON(data);
            for (i = 0; i < arr.length; i++) {
                var jo = arr[i];
                var o = mod.create(jo);
                s.push(o);
            }
            return s;
        });
    },
    create: function(mod, obj) {
        return $.post(App.Config.server,
                    {action: 'create', table: mod.mclass},
        "json").then(function(data) {
            var x = $.parseJSON(data);
            var o = obj;
            o.action = 'update';
            o.table  = mod.mclass;
            o.id     = x.id;
            return $.post(App.Config.server, o, "json").then(function(data) {
                var x = $.parseJSON(data);
                return x;
            });
        });
    },
    update: function(mod, obj) {
        return $.post(App.Config.server,
                    {action: 'read', table: mod.mclass, field: 'id', data: obj.id},
        "json").then(function(data) {
            var x = $.parseJSON(data);
            var o = obj;
            o.action = 'update';
            o.table  = mod.mclass;
            o.id     = x[0].id; // update only work on one record 
            return $.post(App.Config.server, o, "json").then(function(data) {
                var x = $.parseJSON(data);
                return x;
            });
        });
    },    
});

App.Store = Ember.Object.extend({
    adapter:     App.RESTAdapter.create(),
    table2class: function(t) {
        return toTitleCase(t);
    },
    find:        function(mod, obj) {
        return this.adapter.find(mod, obj);
    },
    create:      function(mod, obj) {
        return this.adapter.create(mod, obj);
    },
    update:      function(mod, obj) {
        return this.adapter.update(mod, obj);
    },
    packAndCreateUpdate: function(obj, mod, ctrl, create) {        
        var fields = Ember.keys(Ember.meta(mod.proto()).descs);
        $.each(fields, function(key, val) {
            var x = ctrl.get(val);
            if (typeof x !== 'undefined') {
                if (isNaN(x)) {
                    if (!x.trim()) {
                        return;
                    }
                }
            }
            eval('obj.' + val + ' = "' + x + '";');
        });
        if (create) {
            this.create(mod, obj);
        } else {
            this.update(mod, obj);
        }
        $.each(fields, function(key, val) {
            ctrl.set(val, '');
        });
    }
});

App.store = App.Store.create({});
